package com.zhaoyang;
import com.mojang.authlib.GameProfile;
import com.zhaoyang.utils.YangBukkitUtils;
import net.minecraft.server.v1_12_R1.EntityPlayer;
import net.minecraft.server.v1_12_R1.MinecraftServer;
import net.minecraft.server.v1_12_R1.PlayerInteractManager;
import net.minecraft.server.v1_12_R1.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_12_R1.CraftServer;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.UUID;

public final class Demo extends JavaPlugin {

    @Override
    public void onEnable() {
        // 获取已加载的所有插件
        YangBukkitUtils.getServerVersion();
//        Bukkit.getPluginCommand("spawnfakeplayer").setExecutor(this);
        // 假设玩家名称为 "FakePlayer"，生成在世界的 (0, 100, 0) 位置
        String playerName = "FakePlayer";
        Location location = new Location(Bukkit.getWorlds().get(0), 0, 100, 0);

        // 生成假玩家
        spawnFakePlayer(playerName, location);
    }

    public static void spawnFakePlayer(String playerName, Location location) {
        MinecraftServer minecraftServer = ((CraftServer) Bukkit.getServer()).getServer();
        WorldServer worldServer = ((CraftWorld) location.getWorld()).getHandle();

        // 创建新的 UUID 作为假玩家的唯一标识符
        UUID uuid = UUID.randomUUID();

        // 创建假玩家的 GameProfile
        GameProfile gameProfile = new GameProfile(uuid, playerName);

        // 创建假玩家实体并设置位置
        EntityPlayer fakePlayer = new EntityPlayer(minecraftServer, worldServer, gameProfile, new PlayerInteractManager(worldServer));

        // 设置假玩家的属性，例如名称和位置
        fakePlayer.setCustomName(playerName);
        fakePlayer.setCustomNameVisible(true);
        fakePlayer.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());

        // 将假玩家添加到世界中
        worldServer.addEntity(fakePlayer);

        // 发送假玩家进入服务器的信息
        Player player = fakePlayer.getBukkitEntity();
        Bukkit.getOnlinePlayers().forEach(p -> p.hidePlayer(player));
    }

    public static void despawnFakePlayer(String playerName) {
        // 根据玩家名称查找假玩家并将其移除
        Player player = Bukkit.getPlayer(playerName);
        if (player != null) {
            player.remove();
        }
    }
    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
